package com.ruoyi.common.sql;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class Query implements SqlBuilder {
    protected final List<Table> tables = new ArrayList<>();
    protected final List<String> columns = new ArrayList<>();
    protected Condition where;
    protected final List<OrderBy> orderBys = new ArrayList<>();
    protected GroupBy groupBy;
    protected Having having;
    protected Integer limit;
    protected Integer offset;

    public abstract static class Builder<T extends Builder<T>> {
        protected final Query query;

        protected Builder(Query query) {
            this.query = query;
        }

        public T table(Table table) {
            query.tables.add(table);
            return self();
        }

        public T column(String column) {
            query.columns.add(column);
            return self();
        }

        public T columns(String... columns) {
            Collections.addAll(query.columns, columns);
            return self();
        }

        public T where(Condition condition) {
            query.where = condition;
            return self();
        }

        public T orderBy(OrderBy orderBy) {
            query.orderBys.add(orderBy);
            return self();
        }

        public T groupBy(GroupBy groupBy) {
            query.groupBy = groupBy;
            return self();
        }

        public T having(Having having) {
            query.having = having;
            return self();
        }

        public T limit(int limit) {
            query.limit = limit;
            return self();
        }

        public T offset(int offset) {
            query.offset = offset;
            return self();
        }

        protected abstract T self();

        public abstract Query build();
    }
}
